package com.puhui.education.fragment.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.puhui.education.R;
import com.puhui.education.activity.MyMessageAcy;
import com.puhui.education.activity.RecentSearchAcy;
import com.puhui.education.adapter.ViewHolder;
import com.puhui.education.adapter.base.recyclerview.MultiItemCommonAdapter;
import com.puhui.education.adapter.base.recyclerview.MultiItemTypeSupport;
import com.puhui.education.application.BaseApplication;
import com.puhui.education.bean.CourseLittleInfo;
import com.puhui.education.bean.HomeFragmentRecyclerViewData;
import com.puhui.education.bean.HorizontalBarInfo;
import com.puhui.education.fragment.BaseFrag;
import com.puhui.education.util.ComputeSizeByScaleUtil;
import com.puhui.education.util.DynamicLayoutParamsUtil;
import com.puhui.education.util.IntentUtil;
import com.puhui.education.view.BannerLayout;
import com.puhui.education.view.CourseIntroductionView;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/27.
 */
public class HomeFrag extends BaseFrag {

    @Bind(R.id.rv_displayCourseInfo_homeFrag)
    RecyclerView rv_displayCourseInfo;
    @Bind(R.id.bl_ad_homeFrag)
    BannerLayout bl_ad;
    private Activity activity;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.frag_home, null);
        }
        ButterKnife.bind(this, view);
        initData();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = (Activity) context;
    }

    @OnClick({R.id.sv_homeFrag,R.id.et_searchContent_searchViewCv})
    public void onSearchCourseClick() {
        IntentUtil.startActivity(activity, RecentSearchAcy.class);
    } @OnClick(R.id.tvp_message_homeFrag)
    public void onMessageClick() {
        IntentUtil.startActivity(activity,MyMessageAcy.class);
    }

    @Override
    protected void initData() {
        super.initData();
        final List<String> urls = new ArrayList<>();
        urls.add("http://www.pelway.cn/images/slider/1.jpg");
        urls.add("http://www.pelway.cn/images/slider/2.jpg");
        urls.add("http://www.pelway.cn/images/slider/3.jpg");
        urls.add("http://www.pelway.cn/images/slider/4.jpg");
        urls.add("http://www.pelway.cn/images/slider/5.jpg");
        bl_ad.setViewUrls(urls);
        List<HomeFragmentRecyclerViewData> dataList = new ArrayList<>();
        HorizontalBarInfo horizontalBarInfo = new HorizontalBarInfo(R.mipmap.course_selected, "普惠推荐");
        HomeFragmentRecyclerViewData data1 = new HomeFragmentRecyclerViewData(HomeFragmentRecyclerViewData.TIP, horizontalBarInfo, null);

        List<CourseLittleInfo> courseLittleInfoList = new ArrayList<>();
        courseLittleInfoList.add(new CourseLittleInfo(R.mipmap.course, "初二总复习", "¥688"));
        courseLittleInfoList.add(new CourseLittleInfo(R.mipmap.course, "初二总复习", "¥688"));
        HomeFragmentRecyclerViewData data2 = new HomeFragmentRecyclerViewData(HomeFragmentRecyclerViewData.COURSE, null, courseLittleInfoList);
        dataList.add(data1);
        dataList.add(data2);
        rv_displayCourseInfo.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        rv_displayCourseInfo.setAdapter(new HomeFragRecyclerViewAdapter(BaseApplication.getContext(), dataList));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}

class HomeFragRecyclerViewAdapter extends MultiItemCommonAdapter<HomeFragmentRecyclerViewData> {

    public HomeFragRecyclerViewAdapter(Context context, List<HomeFragmentRecyclerViewData> datas) {
        super(context, datas, new MultiItemTypeSupport<HomeFragmentRecyclerViewData>() {

            @Override
            public int getLayoutId(int itemType) {
                if (itemType == HomeFragmentRecyclerViewData.TIP) {
                    return R.layout.item_rv_tip_home_frag;
                } else {
                    return R.layout.item_rv_course_little_info;
                }
            }

            @Override
            public int getItemViewType(int position, HomeFragmentRecyclerViewData homeFragmentRecyclerViewData) {
                return homeFragmentRecyclerViewData.getLayoutType();
            }
        });
    }


    @Override
    public void convert(ViewHolder holder, HomeFragmentRecyclerViewData homeFragmentRecyclerViewData) {
        switch (holder.getLayoutId()) {
            case R.layout.item_rv_tip_home_frag:
                PercentRelativeLayout prl = holder.getView(R.id.prl_container_itemRvTip);
                prl.setLayoutParams(DynamicLayoutParamsUtil.createViewGroupLayoutParams(DynamicLayoutParamsUtil.WIDTH_MATCH, -1f, 0.085f));
                break;
            case R.layout.item_rv_course_little_info:
                CourseIntroductionView leftView = holder.getView(R.id.cv_courseIntroductionLeft_itemRvCourse);
                CourseIntroductionView rightView = holder.getView(R.id.cv_courseIntroductionRight_itemRvCourse);

                LinearLayout.LayoutParams leftParams = DynamicLayoutParamsUtil.createLinearLayoutParams(DynamicLayoutParamsUtil.BOTH_NO_MATCH, 0f, 0.3f);
                leftParams.weight = 1;
                leftParams.leftMargin = ComputeSizeByScaleUtil.computeSize(ComputeSizeByScaleUtil.SCREEN_WIDTH, 0.025f);
                leftParams.rightMargin = ComputeSizeByScaleUtil.computeSize(ComputeSizeByScaleUtil.SCREEN_WIDTH, 0.0125f);
                leftView.setLayoutParams(leftParams);

                LinearLayout.LayoutParams rightParams = DynamicLayoutParamsUtil.createLinearLayoutParams(DynamicLayoutParamsUtil.BOTH_NO_MATCH, 0f, 0.3f);
                rightParams.weight = 1;
                rightParams.leftMargin = ComputeSizeByScaleUtil.computeSize(ComputeSizeByScaleUtil.SCREEN_WIDTH, 0.0125f);
                rightParams.rightMargin = ComputeSizeByScaleUtil.computeSize(ComputeSizeByScaleUtil.SCREEN_WIDTH, 0.025f);
                rightView.setLayoutParams(rightParams);
                break;
        }
    }
}
